package com.shaiMiMi;

import java.net.Socket;
import java.util.Vector;

import android.content.Context;
import android.widget.Toast;



public abstract class ZPackReactor
{
	public	abstract void DealPackage(ZPackage pack, Socket sock);
	public	abstract String GetCarePackType();
};

class ZPackStrReactor extends ZPackReactor{	//测试
	private Context mContext = null;

	public ZPackStrReactor(Context context) {
		mContext = context;
	}

	public String GetCarePackType() {
		return "ZPackage_Str";
	}

	public void DealPackage(ZPackage pack, Socket sock) {
		ZPackage_Str package_Str = (ZPackage_Str)pack;
		if (null != mContext) {
			if (!ConnectInfo.GetInstance().IsTestMode()) { // 测试压力模式不toast
				Toast toast = Toast.makeText(mContext, package_Str.GetStr(),
						Toast.LENGTH_LONG);
				toast.show();
			}
		}
	}
}

class ZTcpPackReactorMgr
{
	private Vector<ZPackReactor>  mVecRector = new Vector<ZPackReactor>();

	public void DealPackage(ZPackage pack, Socket sock){
		String sPackType = pack.GetPackageType();
		int iLastDotIndex = sPackType.lastIndexOf(".");
		sPackType = sPackType.substring(iLastDotIndex+1);
		int iSize = mVecRector.size();
		ZPackReactor packReactor = null;
		for (int i = 0; i < iSize; i++) {
			packReactor = mVecRector.get(i);
			if (0 == sPackType.compareTo(packReactor.GetCarePackType())) {
				packReactor.DealPackage(pack, sock);
			}
		}
	}

	public void RegReactor(ZPackReactor reactor){
		mVecRector.add(reactor);
	}

	public void UnRegReactor(String reactorType){
		int iSize = mVecRector.size();
		ZPackReactor curReactor = null;
		for (int i = 0; i < iSize; ) {
			curReactor = mVecRector.get(i);
			if (0 == reactorType.compareTo(curReactor.GetCarePackType())) {
				mVecRector.remove(i);
			}
			else {
				i++;
			}
		}
	}
};